/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DomainModel;

import java.util.ArrayList;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Mershack
 */
public class ManageEmail {

    private static final String HOST = "smtp.gmail.com";
    private static final String USERNAME = "grs.team2";
    private static final String PASSWORD = "grsTeam2123";
    private static final String FROM = "grs.team2@gmail.com";

    /**
     * Sends emails to the array of recipients through an smtp mail server.
     *
     * @param recipients string array of email addresses to send email to
     * @param subject the subject of the email
     * @param text the main message of the email
     * @throws MessagingException
     */
    public boolean sendEmail(ArrayList<String> recipients, String subject,
            String text) {

        //set all the properties
        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", HOST);
        props.put("mail.smtp.user", FROM);
        props.put("mail.smtp.password", PASSWORD);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        try {
            Session session = Session.getDefaultInstance(props, null);
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(FROM));

            ArrayList<InternetAddress> sendTo = new ArrayList<InternetAddress>();

            // To get the array of addresses
            for (int i = 0; i < recipients.size(); i++) {
                sendTo.add(new InternetAddress(recipients.get(i)));
            }

            for (int i = 0; i < sendTo.size(); i++) {
                message.addRecipient(Message.RecipientType.TO, sendTo.get(i));
            }
            message.setSubject(subject);
            message.setText(text);
            Transport transport = session.getTransport("smtp");
            transport.connect(HOST, FROM, PASSWORD);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //used for html URL link in password reset, single recipient
    public boolean sendHtmlEmail(String recipient, String subject,
            String text) {

        //set all the properties
        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", HOST);
        props.put("mail.smtp.user", FROM);
        props.put("mail.smtp.password", PASSWORD);
        props.put("mail.smtp.port", "587");
//        props.put("mail.smtp.port", "467");
        props.put("mail.smtp.auth", "true");

        try {
            Session session = Session.getDefaultInstance(props, null);
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(FROM));

            message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));

            message.setSubject(subject);
            message.setContent(text, "text/html");
            Transport transport = session.getTransport("smtp");
            transport.connect(HOST, FROM, PASSWORD);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

}
